import 'dart:async';

import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/model/building_model.dart';
import 'package:ls_work/model/health/health_model.dart';

import '../../a_yyt/models/floor_list_entity.dart';
import '../../net/response/new_health/health_record_list_data.dart';

class NewHealthRecordListViewModel
    extends BaseListViewModel<HealthRecordListData> {
  NewHealthRecordListViewModel(BuildContext context) : super(context);

  var model = HealthModel();

  var searchWords = "";

  var buildingModel = BuildingModel();

  List<FloorEntity> buildingList = [];

  FloorEntity? selectedBuilding;

  FloorInfo? selectedFloor;

  var signItemList = [
    PickerDataWithIdModel(pickName: '身高', pickId: '1',),
    PickerDataWithIdModel(pickName: '体重', pickId: '2'),
    PickerDataWithIdModel(pickName: '心率', pickId: '3'),
    PickerDataWithIdModel(pickName: '血氧', pickId: '4'),
    PickerDataWithIdModel(pickName: '脉率', pickId: '5'),
    PickerDataWithIdModel(pickName: '体温', pickId: '6'),
    PickerDataWithIdModel(pickName: '血压', pickId: '7'),
    PickerDataWithIdModel(pickName: '呼吸', pickId: '9'),
    PickerDataWithIdModel(pickName: '大便', pickId: '10'),
    PickerDataWithIdModel(pickName: '血糖（早餐前）', pickId: '11'),
    PickerDataWithIdModel(pickName: '血糖（早餐后）', pickId: '12'),
    PickerDataWithIdModel(pickName: '血糖（午餐前）', pickId: '13'),
    PickerDataWithIdModel(pickName: '血糖（午餐后）', pickId: '14'),
    PickerDataWithIdModel(pickName: '血糖（晚餐前）', pickId: '15'),
    PickerDataWithIdModel(pickName: '血糖（晚餐后）', pickId: '16'),
    PickerDataWithIdModel(pickName: '血糖（睡前）', pickId: '17'),
    PickerDataWithIdModel(pickName: '血糖（其他）', pickId: '18'),
    PickerDataWithIdModel(pickName: '尿量', pickId: '20'),
    PickerDataWithIdModel(pickName: '小便次数', pickId: '21'),
    PickerDataWithIdModel(pickName: '腋温', pickId: '22'),
    PickerDataWithIdModel(pickName: '肛温', pickId: '23'),
    PickerDataWithIdModel(pickName: '电子', pickId: '24'),
    PickerDataWithIdModel(pickName: '血白蛋白', pickId: '25'),
    PickerDataWithIdModel(pickName: '前白蛋白', pickId: '26'),
    PickerDataWithIdModel(pickName: '血红蛋白', pickId: '27'),
    PickerDataWithIdModel(pickName: '疼痛评估', pickId: '28'),
    PickerDataWithIdModel(pickName: 'c-反应蛋白', pickId: '29'),
    PickerDataWithIdModel(pickName: '进餐', pickId: '30'),
    PickerDataWithIdModel(pickName: '补水', pickId: '31'),
    PickerDataWithIdModel(pickName: '输液', pickId: '32'),
  ];

  var selectedItems = <PickerDataWithIdModel>[];

  @override
  Future<List<HealthRecordListData>> requestListData() async {
    if (buildingList.isEmpty) {
      buildingList = await buildingModel.getBuildingList(page: 1, rows: 500);
    }
    var ids = selectedItems.map((e) => e.pickId).join(",");
    return await model.healthRecord(
      buildingId: selectedBuilding?.id,
      floorId: selectedFloor?.id,
      customerNameLike: searchWords,
      collectionTime: startDateStr,
      dataTypeIdStr: ids,
      page: page,
      rows: pageLength,
    );
  }

  @override
  void onCreated() {
    super.onCreated();
    requestRefresh();
  }

  void chooseBuilding() {
    DataPickerView(context, buildingList, currentFirstData: selectedBuilding,
        onSubmitListener: (p1, p2, p3) {
      if (selectedBuilding != buildingList[p1]) {
        selectedBuilding = buildingList[p1];
        selectedFloor = null;
        notifyListeners();
        requestRefresh();
      }
    }).show();
  }

  void chooseFloor() {
    if (selectedBuilding == null) {
      showToast("请先选择楼宇");
      return;
    }
    DataPickerView(context, selectedBuilding?.floorList??[],
        currentFirstData: selectedFloor, onSubmitListener: (p1, p2, p3) {
      selectedFloor = selectedBuilding!.floorList![p1];
      notifyListeners();
      requestRefresh();
    }).show();
  }

  void reset() {
    searchWords = "";
    selectedBuilding = null;
    selectedFloor = null;
    startDateStr = "";
    startDateTime = null;
    selectedItems.clear();
    notifyListeners();
    requestRefresh();
  }

  Timer? _timer;

  void delayRequest() {
    if (_timer != null) {
      _timer?.cancel();
      _timer = null;
    }
    _timer = Timer(Duration(seconds: 1), () {
      requestRefresh();
    });
  }
}
